<style scoped>
.publish {
  line-height: 30px;
  font-size: 14px;
  color: #fff;
  background: #258ffb;
  padding: 0 15px;
  -webkit-border-radius: 30px;
  border-radius: 30px;
  height: 30px;
  margin: 0 10px;
  cursor: pointer;
}
.publish:hover {
  background-color: #66b1ff;
}
.publish /deep/ .el-loading-spinner {
  margin-top: -15px;
}
.publish /deep/ .el-loading-spinner .circular {
  width: 30px;
  height: 30px;
}
</style>
<!--  -->
<template>
  <div class="publish" @click.stop="publish" v-loading="loading">发布
  </div>
</template>

<script>
import PubSub from 'pubsub-js'
import { mapState } from 'vuex'
import crypto from '@/core/crypto.js'
// import * as WindowType from '@/config/windowtype.js'
// import VueFunctions from '@/core/VueFunctions.js'
import * as PubsubType from '@/config/pubsubtype.js'

import BackendjsFunctions from '@/core/BackendjsFunctions.js'

export default {
  created () {
    // 需要固定的主容器组件索引数组传入iframe中，而后置为空
    // VueFunctions.method(WindowType.GET_PUBLISH_PAGE_DATA_STRONGE, (callback) => {
    //   let mainBoxFixed = JSON.parse(JSON.stringify(this.mainBoxFixed))
    //   // this.mainBoxFixed = []
    //   callback(mainBoxFixed, this.scrollTop)
    // })
  },
  mounted () {
    PubSub.subscribe(PubsubType.PUBSUB_IFRAME_LOADING_OVER, (event, data) => {
      this.loading = false
    })
  },
  computed: {
    ...mapState([
      'design'
    ])
  },
  data () {
    return {
      mainBoxFixed: [],
      // 滚动条位置
      scrollTop: 0,
      loading: true
    }
  },
  methods: {
    // 发布页面
    publish () {
      if (this.loading) {
        return
      }
      this.loading = true
      let dataBody = false
      try {
        dataBody = BackendjsFunctions.publish()
      } catch (error) {
        console.error(error)
      }
      if (dataBody === 'nopage') {
        this.$message.warning('没需要发布的页面数据！')
        this.loading = false
        return
      }
      if (!dataBody) {
        // 为空，没有需要提交的数据
        this.loading = false
        return
      }
      let postDataBody = crypto.en(dataBody)
      this.$jpost('/action/publish/save', {
        data: postDataBody,
        utime: new Date().getTime()
      }).then(response => {
        if (response === null) {
          PubSub.publish(PubsubType.PUBSUB_DESIGN_REFRESH_PAGE)
        }
        // 刷新当前编辑页面
        // this.$store.dispatch('designRefreshPage', true)
      }).catch((e) => {
        this.loading = false
      })
      // BackendjsFunctions.publish((dataBody) => {
      //   let postDataBody = crypto.en(dataBody)
      //   PubSub.publish(PubsubType.PUBSUB_CHANGE_DESIGN_IFRAME_LOADING, true)
      //   this.$jpost('/action/publish/save', {
      //     dataBody: postDataBody,
      //     utime: new Date().getTime()
      //   }).then(response => {
      //     if (response === null) {
      //       PubSub.publish(PubsubType.PUBSUB_DESIGN_REFRESH_PAGE)
      //     }
      //     // 刷新当前编辑页面
      //     // this.$store.dispatch('designRefreshPage', true)
      //   })
      // })
    }
  }
}

</script>
